﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibKetoan;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmMauketchuyen.
	/// </summary>
	public class frmMauketchuyen : System.Windows.Forms.Form
	{
		private DataTable dtmkc=new DataTable();
		private AccessData a=new AccessData();	
		private string s_yy="",s_kysl="";
		private int i_userid=0;
		private string s_mmyy="",s_yyyy="";

		private Brush disabledBackBrush;
		private Brush disabledTextBrush;
		private Brush alertBackBrush;
		private Brush alertTextBrush;
		private Font alertFont;
		private Brush currentRowBackBrush;
		private Font currentRowFont;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.CheckBox chkAll;
		private System.Windows.Forms.Button butcapnhat;
		private System.Windows.Forms.Button butKetthuc;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmMauketchuyen(AccessData acc, string yy,string kysl,int userid,string mmyy,string yyyy)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			a=acc;
			s_yy=yy;
			s_kysl=kysl;
			i_userid=userid;
			s_mmyy=mmyy;
			s_yyyy=yyyy;
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmMauketchuyen));
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.chkAll = new System.Windows.Forms.CheckBox();
            this.butcapnhat = new System.Windows.Forms.Button();
            this.butKetthuc = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGrid1
            // 
            this.dataGrid1.AlternatingBackColor = System.Drawing.Color.Navy;
            this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGrid1.BackColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.CaptionFont = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.CaptionForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.DataMember = "";
            this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.GridLineColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
            this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dataGrid1.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
            this.dataGrid1.Location = new System.Drawing.Point(0, -16);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.RowHeaderWidth = 10;
            this.dataGrid1.SelectionBackColor = System.Drawing.Color.CadetBlue;
            this.dataGrid1.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.Size = new System.Drawing.Size(631, 273);
            this.dataGrid1.TabIndex = 141;
            // 
            // chkAll
            // 
            this.chkAll.Appearance = System.Windows.Forms.Appearance.Button;
            this.chkAll.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.chkAll.Location = new System.Drawing.Point(48, 0);
            this.chkAll.Name = "chkAll";
            this.chkAll.Size = new System.Drawing.Size(23, 21);
            this.chkAll.TabIndex = 150;
            this.chkAll.Text = "...";
            this.chkAll.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.chkAll.CheckedChanged += new System.EventHandler(this.chkAll_CheckedChanged);
            // 
            // butcapnhat
            // 
            this.butcapnhat.Image = ((System.Drawing.Image)(resources.GetObject("butcapnhat.Image")));
            this.butcapnhat.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butcapnhat.Location = new System.Drawing.Point(475, 363);
            this.butcapnhat.Name = "butcapnhat";
            this.butcapnhat.Size = new System.Drawing.Size(72, 25);
            this.butcapnhat.TabIndex = 151;
            this.butcapnhat.Text = " &Cập nhật";
            this.butcapnhat.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butcapnhat.Click += new System.EventHandler(this.butcapnhat_Click);
            // 
            // butKetthuc
            // 
            this.butKetthuc.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
            this.butKetthuc.Image = ((System.Drawing.Image)(resources.GetObject("butKetthuc.Image")));
            this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKetthuc.Location = new System.Drawing.Point(548, 363);
            this.butKetthuc.Name = "butKetthuc";
            this.butKetthuc.Size = new System.Drawing.Size(70, 25);
            this.butKetthuc.TabIndex = 152;
            this.butKetthuc.Text = "&Kết thúc";
            this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
            // 
            // frmMauketchuyen
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(630, 400);
            this.Controls.Add(this.butKetthuc);
            this.Controls.Add(this.butcapnhat);
            this.Controls.Add(this.chkAll);
            this.Controls.Add(this.dataGrid1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.Name = "frmMauketchuyen";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "frmMauketchuyen";
            this.Load += new System.EventHandler(this.frmMauketchuyen_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion

		private void frmMauketchuyen_Load(object sender, System.EventArgs e)
		{			
			load_mauketchuyen();
			AddGridTableStyle();

			this.disabledBackBrush = new SolidBrush(Color.FromArgb(255,255,192));
			this.disabledTextBrush = new SolidBrush(Color.FromArgb(255,0,0));

			this.alertBackBrush = new SolidBrush(SystemColors.HotTrack);
			this.alertFont = new Font(this.dataGrid1.Font.Name, this.dataGrid1.Font.Size, FontStyle.Bold);
			this.alertTextBrush = new SolidBrush(Color.White);
			
			this.currentRowFont = new Font(this.dataGrid1.Font.Name, this.dataGrid1.Font.Size, FontStyle.Regular);
			this.currentRowBackBrush = new SolidBrush(Color.FromArgb(0,255, 255));
		}
		private void SetCellFormat(object sender, DataGridFormatCellEventArgs e)
		{
			try
			{
				bool discontinued = (bool) ((e.Column != 0) ? this.dataGrid1[e.Row, 0] : e.CurrentCellValue);
				if(e.Column > 0 && (bool)(this.dataGrid1[e.Row, 0]))//discontinued)
				{
					e.ForeBrush = this.disabledTextBrush;
				}
				else if(e.Column > 0 && e.Row == this.dataGrid1.CurrentRowIndex)
				{
					e.BackBrush = this.currentRowBackBrush;
					e.TextFont = this.currentRowFont;
				}
				if (decimal.Parse(dataGrid1[e.Row,3].ToString())!=0) e.ForeBrush=new SolidBrush(Color.Orange);
			}
			catch{}
		}
		private void RefreshRow(int row)
		{
			Rectangle rect = this.dataGrid1.GetCellBounds(row, 0);
			rect = new Rectangle(rect.Right, rect.Top, this.dataGrid1.Width, rect.Height);
			this.dataGrid1.Invalidate(rect);
			
		}
		private void BoolValueChanged(object sender, BoolValueChangedEventArgs e)
		{
			this.dataGrid1.EndEdit(this.dataGrid1.TableStyles[0].GridColumnStyles["Discontinued"],e.Row, false);
			RefreshRow(e.Row);
			this.dataGrid1.BeginEdit(this.dataGrid1.TableStyles[0].GridColumnStyles["Discontinued"],e.Row);			
		}
		private void AddGridTableStyle()
		{
			DataGridTableStyle ts =new DataGridTableStyle();			
			ts.MappingName = dtmkc.TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.Gainsboro;
			ts.HeaderForeColor = Color.Blue;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			//ts.ReadOnly=false;
			ts.RowHeaderWidth=15;	
			ts.PreferredRowHeight=22;
					
			DataGridTextBoxColumn TextCol=new DataGridTextBoxColumn();			
			TextCol.MappingName = "id";
			TextCol.HeaderText = "";
			TextCol.Width = 0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "stt";
			TextCol.HeaderText = "STT";
			TextCol.ReadOnly=true;
			TextCol.Width = 30;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);			

			FormattableBooleanColumn discontinuedCol = new FormattableBooleanColumn();
			discontinuedCol.MappingName = "chon";
			discontinuedCol.HeaderText = "Chọn";
			discontinuedCol.Width = 30;
			discontinuedCol.AllowNull = false;

			discontinuedCol.SetCellFormat += new FormatCellEventHandler(SetCellFormat);
			discontinuedCol.BoolValueChanged += new BoolValueChangedEventHandler(BoolValueChanged);
			ts.GridColumnStyles.Add(discontinuedCol);
			dataGrid1.TableStyles.Add(ts);
		
			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName ="tu";
			TextCol.HeaderText = "Từ tk";
			TextCol.NullText="";
			TextCol.ReadOnly=true;
			TextCol.Width = 50;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName ="den";
			TextCol.HeaderText = "Sang tk";
			TextCol.ReadOnly=true;
			TextCol.NullText="";
			TextCol.Width = 50;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ten";
			TextCol.HeaderText = "tên";
			TextCol.ReadOnly=true;
			TextCol.Width = 250;
			TextCol.NullText="";
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "sobt";
			TextCol.HeaderText = "Sổ btoán";
			TextCol.NullText="";
			TextCol.ReadOnly=true;
			TextCol.Width = 50;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "noco";
			TextCol.HeaderText = "Nợ/Có";
			TextCol.ReadOnly=true;
			TextCol.Width = 50;
			TextCol.NullText="";
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);
		}
		private void load_mauketchuyen()
		{
			string sql="";
			sql="select maukc.id as id, maukc.stt as stt,maukc.tu as tu,maukc.den as den,maukc.ten as ten,";
			sql+=" maukc.sobt as sobt,tc.ten as noco,tc.id as tinhchat,tktu.id as idtu,tkden.id as idden";
			sql+=" from "+a.user+".a_mauketchuyen maukc inner join "+a.user+".a_dmtctk tc on maukc.noco=tc.id";
			sql+=" inner join "+a.user+".a_dmtk tktu on maukc.tu = tktu.stk";
			sql+=" inner join "+a.user+".a_dmtk tkden on maukc.den = tkden.stk order by maukc.stt";
			dtmkc=a.get_data(sql).Tables[0];			
			dtmkc.Columns.Add("chon",typeof(bool));
			foreach(DataRow r in dtmkc.Rows) r["chon"]=false;
			dataGrid1.DataSource=dtmkc;
			CurrencyManager cm = (CurrencyManager)BindingContext[dataGrid1.DataSource,dataGrid1.DataMember]; 
			DataView dv = (DataView) cm.List; 
			dv.AllowNew = false; 
		}

		private void chkAll_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==chkAll)				
				foreach(DataRow r in dtmkc.Rows) 
				{				
					r["chon"]=(chkAll.Checked)?true:false;
				}
		}

		private void butcapnhat_Click(object sender, System.EventArgs e)
		{		
			long l_id=0;
			foreach(DataRow r in dtmkc.Select("true"))
			{				
				try
				{
					l_id=int.Parse(a.get_data("select max(id) from "+a.user+".a_ctkc"+s_yy+"").Tables[0].Rows[0][0].ToString())+1;
				}
				catch{l_id=1;}
				if (r["chon"].ToString().Trim().ToLower()=="true")
				{
					//kiem tra mau ket chuyen dang chon da co trong ctkc chua?
					string sql="select * from "+a.user+".a_ctkc"+s_yy+" where sobtoan like '"+r["sobt"].ToString().Trim()+"' and kysl="+s_kysl+"";
					if(a.get_data(sql).Tables[0].Rows.Count==0)						
						a.upd_ctkc(int.Parse(l_id.ToString()),decimal.Parse(l_id.ToString()),s_yy,0,
						r["sobt"].ToString().Trim(),r["ten"].ToString().Trim(),int.Parse(r["tinhchat"].ToString().Trim()),
						int.Parse(r["idtu"].ToString().Trim()),int.Parse(r["idden"].ToString().Trim()),"","","",0,"","","","","","","",0,"",
						int.Parse(s_kysl.ToString().Trim()),"",0,"",i_userid,DateTime.Now);						
				}
			}
			MessageBox.Show("Đã cập nhật!");
		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			this.Close();		
		}
		 
	}
}
